package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.DstChannelCfgMapper;
import cc.rengu.igas.bomp.common.dao.impl.DstChannelCfgMapperImpl;
import cc.rengu.igas.bomp.common.entity.DstChannelCfg;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.ChannelInfoBean;
import cc.rengu.igas.bomp.facade.request.ChannelInfoListQueryRequest;
import cc.rengu.igas.bomp.facade.response.ChannelInfoListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.DstChannelInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.DstChannelInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.BaseProdEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付通道列表查询
 * Created by 赵奎伍
 */
public class ChannelInfoListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ChannelInfoListQueryRequest mchntImageInfoQueryRequest = new ChannelInfoListQueryRequest();
        ConvertUtil.convertOutput(mchntImageInfoQueryRequest);
        return mchntImageInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ChannelInfoListQueryRequest channelInfoListQueryRequest = (ChannelInfoListQueryRequest) request;
        ChannelInfoListQueryResponse channelInfoListQueryResponse = new ChannelInfoListQueryResponse();
        BeanUtil.beanCopy(channelInfoListQueryRequest, channelInfoListQueryResponse);
        BizResponse<ChannelInfoListQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(channelInfoListQueryResponse);

        String instId = channelInfoListQueryRequest.getHeader().getInstId();
        String dstChannelId = channelInfoListQueryRequest.getDstChannelId();

        if (!StringUtil.isEmptyOrNull(dstChannelId)) {
            DstChannelInfoMapper dstChannelInfoMapper = new DstChannelInfoMapperImpl();
            DstChannelInfo dstChannelInfo = dstChannelInfoMapper.selectDstChannelInfoByPrimaryKey(instId, dstChannelId);
            if (null == dstChannelInfo) {
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }

            /* 根据法人行标识查询法人行下所有支付通道信息 */
            DstChannelCfgMapper dstChannelCfgMapper = new DstChannelCfgMapperImpl();
            List<DstChannelCfg> dstChannelCfgList = dstChannelCfgMapper.selectBompDstChannelCfgByDstChannelId(instId, dstChannelId);

            List<ChannelInfoBean> channelInfoList = new ArrayList<>();
            if (CollectionUtils.isEmpty(dstChannelCfgList)) {
                ChannelInfoBean channelInfoBean = new ChannelInfoBean();
                channelInfoBean.setDstChannelId(dstChannelInfo.getDstChannelId());
                channelInfoBean.setDstChannelName(dstChannelInfo.getDstChannelName());
                channelInfoList.add(channelInfoBean);
                channelInfoListQueryResponse.setChannelInfoList(channelInfoList);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                return bizResponse;
            }

            if (!StringUtil.isEmptyOrNull(channelInfoListQueryRequest.getTxnGroup())) {
                String txnGroup = channelInfoListQueryRequest.getTxnGroup();
                String baseProdCode = channelInfoListQueryRequest.getBaseProdCode();
                String dstChannelType = InitParamUtil.getDstChannelTypeByTxnGroup(baseProdCode, channelInfoListQueryRequest.getTxnGroup());
                List<DstChannelCfg> tmpList = dstChannelCfgList.stream().filter(item -> item.getDstChannelType().startsWith(dstChannelType)).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(tmpList)) {
                    if ("2100".equals(txnGroup) && BaseProdEnum.BANK_CARD_PAY.getProdCode().equals(baseProdCode)) {
                        tmpList = dstChannelCfgList.stream().filter(item -> item.getDstChannelType().startsWith("BB0")).collect(Collectors.toList());
                        if (CollectionUtils.isEmpty(tmpList)) {
                            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                            return bizResponse;
                        }
                    } else {
                        bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                        bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                        return bizResponse;
                    }
                } else {
                    //如果是网关支付 累加BB0类型数据
                    if ("2100".equals(txnGroup) && BaseProdEnum.BANK_CARD_PAY.getProdCode().equals(baseProdCode)) {
                        List<DstChannelCfg> bb0List = dstChannelCfgList.stream().filter(item -> item.getDstChannelType().startsWith("BB0")).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(bb0List)) {
                            tmpList.addAll(bb0List);
                        }
                    }
                }
                dstChannelCfgList = tmpList;
            }

            for (DstChannelCfg dstChannelCfg : dstChannelCfgList) {
                ChannelInfoBean channelInfoBean = new ChannelInfoBean();
                channelInfoBean.setDstChannelId(dstChannelInfo.getDstChannelId());
                channelInfoBean.setDstChannelName(dstChannelInfo.getDstChannelName());
                channelInfoBean.setDstChannelType(dstChannelCfg.getDstChannelType());
                channelInfoBean.setDstChannelTypeName(dstChannelCfg.getDstChannelTypeDesc());
                channelInfoBean.setChannelNo(dstChannelCfg.getChannelNo());
                channelInfoBean.setProviderMchntNo(dstChannelCfg.getProviderMchntNo());
                channelInfoList.add(channelInfoBean);
            }

            channelInfoListQueryResponse.setChannelInfoList(channelInfoList);

        } else {
            DstChannelInfoMapper dstChannelInfoMapper = new DstChannelInfoMapperImpl();
            List<DstChannelInfo> dstChannelInfoList = dstChannelInfoMapper.selectDstChannelInfoByInstId(instId);
            if (CollectionUtils.isEmpty(dstChannelInfoList)) {
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }

            List<ChannelInfoBean> channelInfoList = new ArrayList<>();

            for (DstChannelInfo dstChannelInfo : dstChannelInfoList) {


                /* 根据法人行标识查询法人行下所有支付通道信息 */
                DstChannelCfgMapper dstChannelCfgMapper = new DstChannelCfgMapperImpl();
                List<DstChannelCfg> dstChannelCfgList = dstChannelCfgMapper.selectBompDstChannelCfgByDstChannelId(instId, dstChannelId);

                if (CollectionUtils.isEmpty(dstChannelCfgList)) {
                    ChannelInfoBean channelInfoBean = new ChannelInfoBean();
                    channelInfoBean.setDstChannelId(dstChannelInfo.getDstChannelId());
                    channelInfoBean.setDstChannelName(dstChannelInfo.getDstChannelName());
                    channelInfoList.add(channelInfoBean);
                    break;
                }

                for (DstChannelCfg dstChannelCfg : dstChannelCfgList) {
                    ChannelInfoBean channelInfoBean = new ChannelInfoBean();
                    channelInfoBean.setDstChannelId(dstChannelInfo.getDstChannelId());
                    channelInfoBean.setDstChannelName(dstChannelInfo.getDstChannelName());
                    channelInfoBean.setDstChannelType(dstChannelCfg.getDstChannelType());
                    channelInfoBean.setDstChannelTypeName(dstChannelCfg.getDstChannelTypeDesc());
                    channelInfoBean.setChannelNo(dstChannelCfg.getChannelNo());
                    channelInfoBean.setProviderMchntNo(dstChannelCfg.getProviderMchntNo());
                    channelInfoList.add(channelInfoBean);
                }
            }

            channelInfoListQueryResponse.setChannelInfoList(channelInfoList);
        }

        /* 按照接口转换基础产品信息并响应给请求方 */
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ChannelInfoListQueryResponse channelInfoListQueryResponse = (ChannelInfoListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(channelInfoListQueryResponse);
    }


}
